package com.adinnet.admin.product.controller;


import com.adinnet.admin.common.service.CommonService;
import com.adinnet.admin.product.model.form.ProductForm;
import com.adinnet.admin.product.model.vo.ProductReq;
import com.adinnet.admin.product.model.vo.ProductResp;
import com.adinnet.admin.product.service.ProductService;
import com.adinnet.core.BaseController;
import com.adinnet.core.JsonResult;
import com.adinnet.core.PageResult;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author liukp
 * @since 2020-07-02
 */
@Controller
@RequestMapping("admin/product")
public class ProductController extends BaseController {

    @Autowired
    private ProductService productService;

    @Autowired
    private CommonService commonService;

    @GetMapping
    public String findLevel(Model model){
//        List<Store> storeList = storeService.getAllList();
//        SysUser sysUser= commonService.getLoginUser();
//        if(sysUser!=null && sysUser.getStoreId().intValue()>0){
//            storeList=storeList.stream().filter(item -> item.getId().intValue()==sysUser.getStoreId().intValue()).collect(Collectors.toList());
//        }
//        model.addAttribute("storeList",storeList);
        return "product/product.html";
    }

    /**
     * 查询商品列表
     * @param productReq
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public PageResult<ProductResp> list(ProductReq productReq){
        return productService.list(productReq);
    }

    /**
     * 新增商品
     * @param productForm
     * @return
     */
    @PostMapping("/addProduct")
    @ResponseBody
    public JsonResult addProduct(ProductForm productForm, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(productForm.getProductName()) || ObjectUtils.isEmpty(productForm.getPicUrl())) {
            error("必填参数不能为空");
        }
        return ok(productService.addProduct(productForm,request));
    }

    /**
     * 修改商品
     * @param productForm
     * @return
     */
    @PostMapping("/updateProduct")
    @ResponseBody
    public JsonResult updateProduct(ProductForm productForm, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(productForm.getProductName()) || ObjectUtils.isEmpty(productForm.getPicUrl())) {
            error("必填参数不能为空");
        }
        return ok(productService.updateProduct(productForm,request));
    }

    /**
     * 商品下架/上架
     */
    @PostMapping("solOut")
    @ResponseBody
    public JsonResult solOutProduct(Integer id,Integer status) {
        if (ObjectUtils.isEmpty(id) && ObjectUtils.isEmpty(status)) {
            error("必填参数为空");
        }
        return ok(productService.solOutProduct(id,status));
    }

    /**
     * 删除商品
     * @param id
     * @return
     */
    @PostMapping("delProduct")
    @ResponseBody
    public JsonResult delProduct(Integer id, HttpServletRequest request) {
        if (ObjectUtils.isEmpty(id)) {
            error("必填参数为空");
        }
        return ok(productService.delProduct(id,request));
    }
}
